package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.KnowledgeBase;
import com.mxpio.erp.common.equipment.service.KnowledgeBaseService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.jpa.query.Order;
import com.mxpioframework.security.util.SecurityUtils;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class KnowledgeBaseServiceImpl extends BaseServiceImpl<KnowledgeBase> implements KnowledgeBaseService {

    @Transactional
    @Override
    public KnowledgeBase save(KnowledgeBase entity){
        if(StringUtils.isEmpty(entity.getFamily())){
            entity.setFamily(UUID.randomUUID().toString().replace("-",""));
        }
        super.save(entity);
        return entity;
    }

    @Transactional
    @Override
    public Result<KnowledgeBase> upgrade(KnowledgeBase entity){
        Long upgradingCount = JpaUtil.linq(KnowledgeBase.class).equal("prevId", entity.getPrevId()).equal("status","2").count();
        if(upgradingCount>0){
            return Result.error("有未审批完成的升版记录，请先审批后再进行操作");
        }
        KnowledgeBase prevKnBase = getById(KnowledgeBase.class,entity.getPrevId());
        entity.setVersion(prevKnBase.getVersion()+1);
        entity.setFamily(prevKnBase.getFamily());
        entity.setStatus("2");
        save(entity);
        return Result.OK();
    }

    @Transactional
    @Override
    public Result<KnowledgeBase> accept(String id){
        Date nowTime = new Date();
        KnowledgeBase knowledgeBase = getById(KnowledgeBase.class,id);
        knowledgeBase.setStatus("1");
        knowledgeBase.setAuditTime(nowTime);
        knowledgeBase.setAuditUser(SecurityUtils.getLoginUsername());
        knowledgeBase.setEffectiveTime(nowTime);
        KnowledgeBase prevKnBase = getById(KnowledgeBase.class,knowledgeBase.getPrevId());
        prevKnBase.setStatus("0");
        prevKnBase.setExpiringTime(nowTime);
        update(knowledgeBase);
        update(prevKnBase);
        return Result.OK();
    }
    @Transactional
    @Override
    public Result<KnowledgeBase> reject(String id,String auditView){
        KnowledgeBase knowledgeBase = getById(KnowledgeBase.class,id);
        knowledgeBase.setStatus("9");
        knowledgeBase.setAuditTime(new Date());
        knowledgeBase.setAuditUser(SecurityUtils.getLoginUsername());
        knowledgeBase.setAuditView(auditView);
        update(knowledgeBase);
        return Result.OK();
    }

    @Transactional
    @Override
    public Result<List<KnowledgeBase>> history(String id){
        KnowledgeBase knowledgeBase = getById(KnowledgeBase.class, id);
        Criteria criteria = historyCriteria(knowledgeBase.getFamily());
        return Result.OK(list(KnowledgeBase.class,criteria));
    }

    @Transactional
    @Override
    public Result<Page<KnowledgeBase>> historyPage(String id, Integer pageSize,Integer pageNo){
        KnowledgeBase knowledgeBase = getById(KnowledgeBase.class, id);
        Criteria criteria = historyCriteria(knowledgeBase.getFamily());
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        return Result.OK(listPage(KnowledgeBase.class,page,criteria));
    }

    private Criteria historyCriteria(String family){
        Criteria criteria = Criteria.create();
        List<String> statusList = Arrays.asList("0","1");
        criteria.addCriterion("status", Operator.IN,statusList);
        criteria.addCriterion("family",Operator.EQ,family);
        criteria.addOrder(new Order("version",true));
        return criteria;
    }

    @Override
    @Transactional
    public Result<?> removeByFamilys(String familys) {
        if (StringUtils.isBlank(familys)){
            return Result.error("入参为空");
        }
        Set<String> familySet = Stream.of(familys.split(",")).collect(Collectors.toSet());
        JpaUtil.lind(KnowledgeBase.class).in("family",familySet).delete();
        return Result.OK();
    }
}
